package com.bjtu.literatureclient.controller.city;


import com.bjtu.literatureclient.domain.city.CityState;
import com.bjtu.literatureclient.service.city.ICityStateService;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/cityState")
public class CityStateController {

    @Autowired
    private ICityStateService cityStateService;

    @RequestMapping(method = RequestMethod.GET)
    public String get() {
       CityState cityState = new CityState();
        List<CityState> cityStateList = this.cityStateService.query(cityState);
        Gson gson = new Gson();
        return gson.toJson(cityStateList);
    }
    
    @RequestMapping(value="/query/id/{param}",method = RequestMethod.GET)
    public String queryByID(@PathVariable("param") int param){
    	System.out.println(param+"---");
    	CityState c = new CityState();
    	c.setCityID(param);
    	List<CityState> cc = this.cityStateService.query(c);
    	System.out.println(cc.size());
    	Gson gson = new Gson();
        return gson.toJson(cc);
    }
    
    @RequestMapping(value="/query/{param}",method = RequestMethod.GET)
    public String queryState(@PathVariable("param") String param){
    	
    	CityState city = new CityState();
    	city.setRegion(param);
    	List<CityState> cityList = this.cityStateService.queryState(city);
    	Gson gson = new Gson();
        return gson.toJson(cityList);
    	
    }
    @RequestMapping(value="/query/{region}/{state}",method = RequestMethod.GET)
    public String queryCity(@PathVariable("region") String region, @PathVariable("state") String state){
    	
    	System.out.println(region+"---"+state);
    	CityState city = new CityState();
    	city.setRegion(region);
    	city.setStateName2(state);
    	List<CityState> cityList = this.cityStateService.queryCity(city);
    	Gson gson = new Gson();
        return gson.toJson(cityList);
    }
    
    @RequestMapping(method = RequestMethod.PUT)
    public void add(@RequestBody CityState city){
    	System.out.println(city.getRegion()+"--"+city.getStateName2());
    	this.cityStateService.insert(city);
    	
    }
    
    @RequestMapping(value="/update",method = RequestMethod.PUT)
    public void update(@RequestBody CityState city){
    	System.out.println(city.getCityName2());
    	this.cityStateService.update(city);
    }
}
